package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import entity.ChargeRule;
import util.Constants;
import util.Validator;

public class AddChargeRuleJPanel extends JPanel {
	private static final long serialVersionUID = 8549141458642551032L;

	public AddChargeRuleJPanel(){
		setLayout(null);
        initUI();
	}
	
	private void initUI(){
		
		JLabel lab1 = new JLabel("名称",SwingConstants.RIGHT);
		lab1.setFont(Constants.font);
		lab1.setBounds(100, 30, 60, 30);
	        
        JTextField lab1input = new JTextField();
        lab1input.setBounds(165, 30, 120, 30);
        
        JLabel lab2 = new JLabel("车位类型",SwingConstants.RIGHT);
        lab2.setFont(Constants.font);
        lab2.setBounds(100, 90, 60, 30);

        JComboBox<String> lab2input = new JComboBox<>();
        lab2input.addItem("-请选择-");
        lab2input.addItem("露天");
        lab2input.addItem("地下");
        lab2input.setBounds(165, 90, 120, 30);
        
        
        JLabel lab3 = new JLabel("时费用",SwingConstants.RIGHT);
        lab3.setFont(Constants.font);
        lab3.setBounds(100, 150, 60, 30);
	        
		JTextField lab3input = new JTextField();
		lab3input.enableInputMethods(false);
		Validator.bind(lab3input,Validator.NUM);
		lab3input.setBounds(165, 150, 120, 30);
		
		JLabel lab4 = new JLabel("月费用",SwingConstants.RIGHT);
		lab4.setFont(Constants.font);
		lab4.setBounds(100, 210, 60, 30);
		
		JTextField lab4input = new JTextField();
		lab4input.enableInputMethods(false);
		Validator.bind(lab4input,Validator.NUM);
		lab4input.setBounds(165, 210, 120, 30);

		JLabel lab5 = new JLabel("生效日期",SwingConstants.RIGHT);
		lab5.setFont(Constants.font);
		lab5.setBounds(100, 270, 60, 30);
		
        JTextField lab5input = new JTextField();
        lab5input.enableInputMethods(false);
        Validator.bind(lab5input,Validator.DATE);
        lab5input.setBounds(165, 270, 120, 30);
        
        JButton btn = new JButton("保存");
		btn.setFont(Constants.font);
		btn.setBounds(165, 330, 120, 30);        
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String lab1inputStr = lab1input.getText().trim();
				String lab2inputStr = lab2input.getSelectedItem().toString();				
				String lab3inputStr = lab3input.getText().trim();
				String lab4inputStr = lab4input.getText().trim();
				String lab5inputStr = lab5input.getText().trim();

				if (Validator.isEmpty(lab1inputStr)) {
					JOptionPane.showMessageDialog(null, "请输入名称！");
					return;
				}
				
				if (lab2input.getSelectedIndex() < 1) {
					JOptionPane.showMessageDialog(null, "请选择车位类型！");
					return;
				}
				
				if (Validator.isEmpty(lab3inputStr)) {
					JOptionPane.showMessageDialog(null, "请输入时费用！");
					return;
				}

				if (Validator.isEmpty(lab4inputStr)) {
					JOptionPane.showMessageDialog(null, "请输入月费用！");
					return;
				}
				
				if (Validator.isEmpty(lab5inputStr)) {
					JOptionPane.showMessageDialog(null, "请输入生效日期！");
					return;
				}
				
				boolean result = false;
				ChargeRule chargeRule = new ChargeRule();
				chargeRule.name = lab1inputStr;
				chargeRule.type = lab2inputStr;
				chargeRule.hourFactor = Double.parseDouble(lab3inputStr);
				chargeRule.monthFactor = Double.parseDouble(lab4inputStr);
				chargeRule.execDate = lab5inputStr;
				result = chargeRule.save();
				if (result) {
					JOptionPane.showMessageDialog(null, "操作成功！");
				}else{
					JOptionPane.showMessageDialog(null, "操作失败！");
				}
			}
		});
		
		add(lab1);
		add(lab1input);
		add(lab2);
		add(lab2input);		
		add(lab3);
		add(lab3input);
		add(lab4);
		add(lab4input);
		add(lab5);
		add(lab5input);
		add(btn);
	}

}
